package cn.tp.ksoft.civil_driver.module.mime;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import cn.tp.ksoft.civil_driver.DriverBaseActivity;
import cn.tp.ksoft.civil_driver.R;
import cn.tp.ksoft.civil_driver.bean.ResultBean;
import cn.tp.ksoft.civil_driver.request.DriverRequest;
import cn.tp.ksoft.civil_driver.request.RequestModules;

/**
 * Created by zhusong on 16/5/12.
 */
public class ModifyPwdActivity extends DriverBaseActivity implements View.OnClickListener, RequestModules{

    private EditText mOldPwdEdt;
    private EditText mPwdEdt;
    private EditText mPwdAgainEdt;

    private DriverRequest request;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.driver_modify_pwd);
        updateTitle(getString(R.string.driver_modify_pwd));
        mOldPwdEdt = (EditText) findViewById(R.id.edt_old_pwd);
        mPwdEdt = (EditText) findViewById(R.id.edt_pwd);
        mPwdAgainEdt = (EditText) findViewById(R.id.edt_pwd_again);
        findViewById(R.id.btn_finish).setOnClickListener(this);

        request = new DriverRequest(this, new WeakHandler(this));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_finish : {
                String oldPwd = mOldPwdEdt.getText().toString();
                if (TextUtils.isEmpty(oldPwd)) {
                    Toast.makeText(this, getString(R.string.driver_please_input_old_pwd),
                            Toast.LENGTH_SHORT).show();
                    mOldPwdEdt.requestFocus();
                    return;
                }

                String pwd = mPwdEdt.getText().toString();
                if (TextUtils.isEmpty(pwd)) {
                    Toast.makeText(this, getString(R.string.driver_please_input_pwd),
                            Toast.LENGTH_SHORT).show();
                    mPwdEdt.requestFocus();
                    return;
                }

                String pwdAgain = mPwdAgainEdt.getText().toString();
                if (TextUtils.isEmpty(pwdAgain)) {
                    Toast.makeText(this, getString(R.string.driver_please_input_pwd),
                            Toast.LENGTH_SHORT).show();
                    mPwdAgainEdt.requestFocus();
                    return;
                }
                if (!pwd.equals(pwdAgain)) {
                    Toast.makeText(this, getString(R.string.driver_tip_pwd_not_equal),
                            Toast.LENGTH_SHORT).show();
                    return;
                }

                request.modifyLoginPwd(oldPwd, pwd);
                break;
            }
        }
    }

    @Override
    protected void handleSuccessResult(int what, @NonNull ResultBean result) {
        switch (what) {
            case MODIFYLOGINPWD : {
                Toast.makeText(this, getString(R.string.driver_tip_modify_pwd_success),
                        Toast.LENGTH_SHORT).show();
                finish();
                break;
            }
        }
    }
}
